from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin

from . import models
from .serializer import BannerSerializer
from django.conf import settings
from django.core.cache import cache


class Banner(GenericViewSet, ListModelMixin):
    queryset = models.Banner.objects.filter(is_delete=False, is_show=True).order_by('-orders')[:settings.BANNER_SIZE]
    serializer_class = BannerSerializer

    def list(self, request, *args, **kwargs):
        banner_data = cache.get('banner_data')
        if banner_data:
            print('走了缓存')
            return Response(data=banner_data)
        res = super().list(request, *args, **kwargs)
        cache.set('banner_data', res.data)
        print('没走缓存')
        return res
